package cn.hou.java.designpattern._09proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//动态代理
public class ProxyDynamic {
	
	private Object target;//代理的对象

	public ProxyDynamic(Object target) {
		this.target = target;
	}
	
	//给目标对象生成代理对象
	public Object getProxyObject(){
		return Proxy.newProxyInstance(
				target.getClass().getClassLoader(), 
				target.getClass().getInterfaces(),
				new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						after();
						Object invoke = method.invoke(target, args);
						return invoke;
					}
				} 
		);
	}
	
	//其他逻辑处理
	public void after(){
		System.out.println("目标对象执行之前");
	}

}
